﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;
using System.Security.Cryptography;
using System.Text;


namespace HospitalSystem.Admin
{
    public partial class IDRegister : System.Web.UI.Page
    {
        public static List<string> x = new List<string>();
        DataTable dt = new DataTable();
        DataTable dx = new DataTable();
        string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
        public static string key = "MDCODE";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack != true)
            {
                RoleLoaded();
            }
            
        }
        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        protected void IDR_Click(object sender, EventArgs e)
        {
            
            SqlConnection scon = new SqlConnection(constr);
            SqlCommand rec = new SqlCommand("INSERT INTO [Employee] (Staff_id,FirstName,LastName,Username,Password,Email,UDATE,Activated) VALUES (@SID,@FN,@LN,@UN,@PW,@EM,@UDT,@ACTD)", scon);
            rec.Parameters.AddWithValue("@SID", SIDTB.Text);
            rec.Parameters.AddWithValue("@FN", FirstnameTB.Text);
            rec.Parameters.AddWithValue("@LN", LastnameTB.Text);
            rec.Parameters.AddWithValue("@UN", IDTB.Text);
            rec.Parameters.AddWithValue("@PW", Encrypt(PWTB.Text, key)); 
            rec.Parameters.AddWithValue("@EM", EmailTB.Text);
            rec.Parameters.AddWithValue("@UDT", DateTime.Now.ToString());
            rec.Parameters.AddWithValue("@ACTD", "0");
            SqlCommand rolerec = new SqlCommand("INSERT INTO [Role_Staff] (Role_id,Staff_id) VALUES (@RID,@SID)", scon);
            rolerec.Parameters.AddWithValue("@SID", SIDTB.Text);
            rolerec.Parameters.AddWithValue("@RID", RoleDDL.SelectedValue.ToString());
            scon.Open();
            try
            {
                if ((SIDTB.Text != string.Empty) && (FirstnameTB.Text != string.Empty) && (LastnameTB.Text != string.Empty) && (IDTB.Text != string.Empty) && (PWTB.Text != string.Empty) && (EmailTB.Text != string.Empty))
                {
                    rec.ExecuteNonQuery();
                    rolerec.ExecuteNonQuery();
                    MessageBox("Register Success!");
                    TxtClear();
                }
                else
                {
                    MessageBox("Invalid Data!");
                }
            }
            catch
            {
                MessageBox("Invalid Data!");
            }
            scon.Close();

 
        }
        private void TxtClear()
        {
            SIDTB.Text = string.Empty;
            FirstnameTB.Text = string.Empty;
            LastnameTB.Text = string.Empty;
            IDTB.Text = string.Empty;
            PWTB.Text = string.Empty;
            EmailTB.Text = string.Empty;
        }
        private void RoleLoaded()
        {
            SqlConnection scon = new SqlConnection(constr);
            SqlCommand rec = new SqlCommand("SELECT Role_id,Role_description FROM [Role]", scon);
            scon.Open();
            SqlDataReader reader = rec.ExecuteReader();
            reader.Read();
            while (reader.Read() != false)
            {
                RoleDDL.Items.Add(new ListItem (reader["Role_description"].ToString(),reader["Role_id"].ToString()));
            }
            reader.Close();
            scon.Close();

        }
        public string Encrypt(string strToEncrypt, string strKey)
        {
            try
            {
                TripleDESCryptoServiceProvider objDESCrypto = new TripleDESCryptoServiceProvider();
                MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();

                byte[] byteHash, byteBuff;
                string strTempKey = strKey;

                byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
                objHashMD5 = null;
                objDESCrypto.Key = byteHash;
                objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB

                byteBuff = ASCIIEncoding.ASCII.GetBytes(strToEncrypt);
                return Convert.ToBase64String(objDESCrypto.CreateEncryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
            }
            catch (Exception ex)
            {
                return "Wrong Input. " + ex.Message;
            }
        }

        //protected void PWTB_TextChanged(object sender, EventArgs e)
        //{
        //    CPWStat.Visible = true;
        //}

        //protected void CPWTB_KeyPress(object sender, System.F
        //{
        //    if (PWTB.Text == CPWTB.Text)
        //    {
        //        CPWStat.ForeColor = System.Drawing.Color.Green;
        //        CPWStat.Text = "ถูกต้อง";
        //    }
        //    else
        //    {
        //        CPWStat.ForeColor = System.Drawing.Color.Red;
        //        CPWStat.Text = "ไม่ถูกต้อง";
        //    }
        //}

        //private string RoleSelected(string RoleDLLText)
        //{
        //    string x;
        //    SqlConnection scon = new SqlConnection(constr);
        //    SqlCommand roleselect = new SqlCommand("SELECT Role_id FROM [Role] WHERE Role_description = @RDDLT", scon);
        //    roleselect.Parameters.AddWithValue("@RDDLT", RoleDDL.SelectedItem.Text);
        //    scon.Open();
        //    SqlDataReader reader = roleselect.ExecuteReader();
        //    reader.Read();
        //    while (reader.Read() != false)
        //    {
        //         x = (reader["Role_description"].ToString());
        //    }
        //   // return x;
        //    reader.Close();
        //    scon.Close();
        //   //
        //}


    }
}